<?php

require_once ("../../config/path_config.php");
include_once (ROOTDIR."common/base_router.php");
require_once (ROOTDIR."config/sys_config.php");
// 样例下载日志
require_once (ROOTDIR."front/model/down_log_model.php");

class TestDataRouter extends BaseRouter {
    public function __construct() {
        parent::__construct();
    }

    function checkData() {
        $userId = BaseRouter::getSession('user_id');
        if ( $userId == null ) {
            $this->setRspStatus(0, -1,"您还未登录" );
            return;
        }

        $downLogModel = new DownLogModel();
        $leftCnt = $downLogModel->checkAndGetLeftDownCnt($userId);
        if ( $leftCnt <= 0 ) {
            $this->setRspStatus(0, -2,"已超过最大下载次数" );
            return;
        }

        $problemId = $this->getReqVal('problemid');
        $fileName = $this->getReqVal('filename');
        if ( $problemId == null || $fileName == null ) {
            $this->setRspStatus(0, -3,"缺少必要参数");
            return;
        }

        // 判断文件是否存在
        // intval和basename是为了避免输入类似于../../的路径，访问其他安全文件
        $filePath = SysConfig::$OJ_DATA."/".intval($problemId)."/".basename($fileName);
        if (! file_exists ( $filePath )) {
            $this->setRspStatus(0, -4,"文件不存在");
            return;
        }
    }

    function downData() {
        $userId = BaseRouter::getSession('user_id');
        if ( $userId == null ) {
            $this->setRspStatus(0, -1,"您还未登录" );
            return;
        }

        $downLogModel = new DownLogModel();
        $leftCnt = $downLogModel->checkAndGetLeftDownCnt($userId);
        if ( $leftCnt <= 0 ) {
            $this->setRspStatus(0, -2,"已超过最大下载次数" );
            return;
        }

        $problemId = $this->getReqVal('problemid');
        $fileName = $this->getReqVal('filename');
        if ( $problemId == null || $fileName == null ) {
            $this->setRspStatus(0, -3,"缺少必要参数");
            return;
        }

        // 判断文件是否存在
        $fileName = basename($fileName);
        $filePath = SysConfig::$OJ_DATA."/".intval($problemId)."/".basename($fileName);
        if (! file_exists ( $filePath )) {
            $this->setRspStatus(0, -4,"文件不存在");
            return;
        }

        // 增加下载次数
        $downLogModel->incDownCnt($userId);
        $fSize = filesize ($filePath);
        //告诉浏览器这是一个文件流格式的文件
        Header ( "Content-type: application/octet-stream" );
        //请求范围的度量单位
        Header ( "Accept-Ranges: bytes" );
        //Content-Length是指定包含于请求或响应中数据的字节长度
        Header ( "Accept-Length: ".$fSize);
        Header ( "Content-Length: ".$fSize);
        //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
        Header ( "Content-Disposition: attachment; filename=".$fileName );
        //以只读和二进制模式打开文件
        $file = fopen ( $filePath, "rb" );
        $content = fread($file, 1024);
        while( strlen($content)!=0 ) {
            echo $content;
            $content = fread($file, 1024);
        }
        fclose ($file);
        exit();
    }

    public function run() {
        $op = $this->getReqVal('op');
        if ( $op == 'down' ) {
            $this->downData();
        }
        else if ( $op == 'check' ) {
            $this->checkData();
        }
    }
}

$router = new TestDataRouter();
$router->init();
$router->run();
$router->finish();
